package com.chzc.util;

import com.chzc.util.enums.StatusCode;
import lombok.Data;

import java.io.Serializable;


/**
 * 统一json处理
 * @Date 2020.06.07
 * @author CV大魔王
 */
@Data
public class Result implements Serializable {

    /** 是否成功 */
    private boolean flag;

    /** 返回码 */
    private Integer code;

    /** 返回消息 */
    private String message;

    /** 返回数据 */
    private Object data;

    public Result(boolean flag, Integer code, String message, Object data) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result(boolean flag, Integer code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;
    }

    public Result() {
        this.flag = true;
        this.code = StatusCode.SUCCESS.getCode();
        this.message = "操作成功!";
    }

    /**
     * 请求成功，返回数据
     * @param data 返回的数据
     */
    public static Result buildSuccess(Object data){
        return new Result(true,StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),data);
    }

    /**
     * 请求成功，不返回数据
     */
    public static Result buildSuccess(){
        return new Result(true,StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
    }

    /**
     * 添加成功
     */
    public static Result addSuccess(){
        return new Result(true,StatusCode.ADD_SUCCESS.getCode(), StatusCode.ADD_SUCCESS.getMessage());
    }

    /**
     * 上传成功
     */
    public static Result ossSuccess(Object data){
        return new Result(true,StatusCode.OSS_SUCCESS.getCode(), StatusCode.OSS_SUCCESS.getMessage(),data);
    }

    /**
     * 删除成功
     */
    public static Result deleteSuccess(){
        return new Result(true,StatusCode.DELETE_SUCCESS.getCode(), StatusCode.DELETE_SUCCESS.getMessage());
    }

    /**
     * 修改成功
     */
    public static Result editSuccess(){
        return new Result(true,StatusCode.EDIT_SUCCESS.getCode(), StatusCode.EDIT_SUCCESS.getMessage());
    }

    /**
     * 查询成功
     */
    public static Result querySuccess(){
        return new Result(true,StatusCode.QUERY_SUCCESS.getCode(), StatusCode.QUERY_SUCCESS.getMessage());
    }

    /**
     * 添加成功,返回数据
     */
    public static Result addSuccess(Object data){
        return new Result(true,
                StatusCode.ADD_SUCCESS.getCode(),
                StatusCode.ADD_SUCCESS.getMessage(),
                data);
    }

    /**
     * 删除成功,返回数据
     */
    public static Result deleteSuccess(Object data){
        return new Result(true,
                StatusCode.DELETE_SUCCESS.getCode(),
                StatusCode.DELETE_SUCCESS.getMessage(),
                data);
    }

    /**
     * 修改成功,返回数据
     */
    public static Result editSuccess(Object data){
        return new Result(true,
                StatusCode.EDIT_SUCCESS.getCode(),
                StatusCode.EDIT_SUCCESS.getMessage(),
                data);
    }

    /**
     * 查询成功,返回数据
     */
    public static Result querySuccess(Object data){
        return new Result(true,
                StatusCode.QUERY_SUCCESS.getCode(),
                StatusCode.QUERY_SUCCESS.getMessage(),
                data);
    }

    /**
     * 请求失败，返回数据
     */
    public static Result buildError(){
        return new Result(false,StatusCode.ERROR.getCode(),StatusCode.ERROR.getMessage());
    }


    /**
     * 请求失败，返回数据
     */
    public static Result buildError(Object data){
        return new Result(false,StatusCode.ERROR.getCode(),StatusCode.ERROR.getMessage(),data);
    }

}